import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.LinkedList;

	
	public class RoundRobinSimulator extends Simulation {
		
		int quantum = (int)(Math.random()*20.0) + 5; // min 4
		
		
		private ArrayList<ProcessNode> legend;
		private boolean context_switch_in_progress;
		
		
		public RoundRobinSimulator(int num_processes, int times_processes) {
			super(num_processes, times_processes, new LinkedList<ProcessNode>());
			legend = new ArrayList<ProcessNode>(processList);
			ProcessNode NPA = new ProcessNode("CS/NPA",this);
			NPA.setColor(Color.LIGHT_GRAY);
			legend.add(NPA);
		}
		public boolean interuptProcessSwitchConditions()	
		{
			if(timeServicedOnCurrentProcess() >= quantum || context_switch_in_progress)
				return true;
			else 
				return false;
		}
		
		//Modified switchProcess to include context switching
		public void switchProcess()
		{
			if((isFirstProcess() || (context_switch_in_progress || noCurrentProcess()) && timeServicedOnCurrentProcess() >= 2)) {
				current_process = processQueue.poll();
				drawNodes.add(new DrawNode(	current_process.getProcessName(), 
						current_process.getColor(),
						timer,
						timer,current_process.getArrivalTime()));
				context_switch_in_progress = false;
			} else if(!context_switch_in_progress) {
				context_switch_in_progress = true;
				noProcess();
			}
		}
		
		private boolean isFirstProcess() 
		{
			if(getTimer() == 0)
				return true;
			else
				return false;
		}
		public void draw(Graphics g) {
			
			int x_mid;
			//0 is top
			int x = 0;
			int width = 0;
			
			//Create Gantt Chart
			for(int i = 0; i < drawNodes.size(); i++) {
				x = SCALAR_X * (SHIFT_X + drawNodes.get(i).getStartTime());
				width = ((SCALAR_X * (SHIFT_X + drawNodes.get(i).getEndTime())) - x);
				
				g.setColor(drawNodes.get(i).getColor());
				g.fillRect(	x, 50, width, 40);
				g.setColor(Color.BLACK);
				g.drawRect( x, 50, width, 40);
			}
			
			if(!drawNodes.isEmpty())
				x = SCALAR_X * (SHIFT_X + drawNodes.get(0).getStartTime());
			
			for(int i = 0; i < legend.size(); i++) {
				
				g.setColor(Color.BLACK);
				g.drawString("   " + legend.get(i).getProcessName()+ " =", x, 35);
				
				x += ((legend.get(i).getProcessName().length()+4)*8);
				
				g.setColor(legend.get(i).getColor());
				g.fillRect(x,26,10,10);
				
				x += 25;
			}
				
			x = 0;
			width = 0;
			//Create Gantt Chart
			for(int i = 0; i < drawNodes.size(); i++) {
				
				x = SCALAR_X * (SHIFT_X + drawNodes.get(i).getStartTime());
				width = ((SCALAR_X * (SHIFT_X + drawNodes.get(i).getEndTime())) - x);
				x_mid = x + (width / 2);
				
				//Draw Service Time
				g.setColor(Color.WHITE);
				g.drawString(String.valueOf(drawNodes.get(i).getServiceTime()), (x_mid - (String.valueOf(drawNodes.get(i).getServiceTime()).length() * TEXT_SHIFT)), 74);
				
				if(drawNodes.get(i).hasProcess()) {
					//Draw Times
					g.setColor(Color.BLACK);
					g.drawString(String.valueOf(drawNodes.get(i).getStartTime()), x, 110);
				}
			}
		}
	}
